
import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
import common from './modules/common'
import tags from './modules/tags'
import errLog from './modules/errLog'
import getters from './getters'
import axios from 'axios'

const state = {
  provincelist:[], 
  citylist:[],
  districtlist:[],
  levelRulelist:[],
  conditionlist:[],
  levellist:[],
  cities:[],
}

const mutations = {
  FETCH_PROVINCE:(state,data)=>{
      state.provincelist = data;
  },
  FETCH_CITY:(state,data)=>{
      state.citylist = data; 
      for(var i = 0; i < data.length;i++){
        state.cities.push(data[i].areaId);
      }
  },
  FETCH_DISTRICT:(state,data)=>{
    state.districtlist = data;
  },
  FETCH_LEVEL_RUlE:(state,data)=>{
      state.levelRulelist = data; 
  },
  FETCH_CUSTOMER_FILTER_INFO:(state,data)=>{
      state.conditionlist = data; 
  },
}

const actions = {
  fetchprovinceLists({commit}){           // 请求公共省份列表
      axios.post('/api/basic/area/province').then((res) => {
          if (res.data.code == 200) {
             commit('FETCH_PROVINCE',res.data.data);
          }
      }).catch((error)=>{
        this.$Message.error("操作失败【" + error + "】");
    })
  },
  fetchcityList({commit},id){                // 请求公共城市列表
      axios.get('/api/basic/area/city/'+id).then((res) => {
        if (res.data.code == 200) {
          commit('FETCH_CITY',res.data.data);
        }
      }).catch((error)=>{
        // this.$Message.error("操作失败【" + error + "】");
    })
  },
  fetchdistrictList({commit},id){                // 请求区列表
    axios.get('/api/basic/area/district/'+id)
      .then((res) => {
        if (res.data.code == 200) {
          commit('FETCH_DISTRICT',res.data.data);
        }
      }).catch((error)=>{
      this.$Message.error("操作失败【" + error + "】");
    })
  },
  fetchlevelRuleList({commit}){                // 请求公共会员等级列表
    axios.post('/api/customer/levelRule/list').then((res)=>{
        if(res.data.code == 200){
            commit('FETCH_LEVEL_RUlE',res.data.data);
        }
    }).catch((error)=>{
        this.$Message.error("操作失败【" + error + "】");
    })
  },
}

Vue.use(Vuex)
const store = new Vuex.Store({
  state,
  actions,
  mutations,
  modules: {
    user,
    common,
    errLog,
    tags
  },
  getters,
})

export default store;
